import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.swing.JOptionPane;

/* 
 * 	Music.java : A class that chooses chooses a music file from hard-disk and plays it.
 *  
 *  @Author : BilCIS
 */
 
public class Music 
{
	// properties
	private String filename;
	
	// constructor
	public Music() 
	{
	}
	
	// Constructor
	public Music( String filename ) 
	{
		this.filename = filename;
	}

	// A method that mutes the sound of the music.
	public void mute ( boolean muteOn )
	{
		// gets mixer
		 javax.sound.sampled.Mixer.Info[] mixers = AudioSystem.getMixerInfo();
		 
		 for(int i=0;i<mixers.length;i++)
		 {
		 	Mixer.Info mixerInfo = mixers[i];
		 	Mixer mixer = AudioSystem.getMixer(mixerInfo);
		 	// creates line for each mix
		 	Line.Info[] lineinfos = mixer.getTargetLineInfo();
		 	for(Line.Info lineinfo : lineinfos)
		    {
		 		Line line = null;
		 		try 
		        {
		 			// opens the line
		        	line = mixer.getLine(lineinfo);
		        	line.open();
		            if(line.isControlSupported(FloatControl.Type.VOLUME))
		            {
		            	FloatControl control = (FloatControl) line.getControl(FloatControl.Type.VOLUME);  
		                // 0.5 being 50%
		                // 0.0 being 0%
		                // 1.0 being 100%
		                if ( muteOn == true ) // you can change the sound with float number
		                	control.setValue((float) 0.0);
		                else
		                	control.setValue((float) 1.0);
		          	}
		         } 
		         catch (LineUnavailableException e)
		         {
		        	JOptionPane.showMessageDialog(null, e.getMessage() );
		         	e.printStackTrace();
		         }
		 		// closes the line for each mix
		 		line.close();
			}
		}
	}
	
	// A method that starts the music with NEW THREAD NOT TO STOP PROGRAM's execution FLOW
	public void play()
	{
		new Thread(){
		      @Override
		      public void run() {
		           try {
			      		 try
			    		 {
			      			 // creates clip and plays it
			    		     Clip clip = AudioSystem.getClip();
			    		     clip.open(AudioSystem.getAudioInputStream(new File(filename)));
			    		     clip.start();
			    		     while (!clip.isRunning())
			    		         Thread.sleep(10); // makes thread sleep to play music more nicely
			    		     while (clip.isRunning())
			    		         Thread.sleep(10);
			    		     clip.close();
			    		 }
			    		 catch (Exception e)
			    		 {
			    			 JOptionPane.showMessageDialog(null, e.getMessage() );
			    		 }
		                 Thread.sleep(30000);
		           } catch (InterruptedException e) 
		           {
		        	   JOptionPane.showMessageDialog(null, e.getMessage() );
		               e.printStackTrace();
		           }
		      };
		}.start();
	}
}